//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    nums: [
    ],
    emptypoint: [510, 510]
  },
  onLoad: function () {
    for (var i = 0; i < 15; i++) {
      var _left;
      var _top;
      if (i % 4 == 0) {
        _left = 0;
        if (parseInt(i / 4) == 0) {
          _top = 0;
        } else if (parseInt(i / 4) == 1) {
          _top = 170;
        } else if (parseInt(i / 4) == 2) {
          _top = 340;
        } else if (parseInt(i / 4) == 3) {
          _top = 510;
        }
      } else if (i % 4 == 1) {
        _left = 170;
        if (parseInt(i / 4) == 0) {
          _top = 0;
        } else if (parseInt(i / 4) == 1) {
          _top = 170;
        } else if (parseInt(i / 4) == 2) {
          _top = 340;
        } else if (parseInt(i / 4) == 3) {
          _top = 510;
        }
      } else if (i % 4 == 2) {
        _left = 340;
        if (parseInt(i / 4) == 0) {
          _top = 0;
        } else if (parseInt(i / 4) == 1) {
          _top = 170;
        } else if (parseInt(i / 4) == 2) {
          _top = 340;
        } else if (parseInt(i / 4) == 3) {
          _top = 510;
        }
      } else if (i % 4 == 3) {
        _left = 510;
        if (parseInt(i / 4) == 0) {
          _top = 0;
        } else if (parseInt(i / 4) == 1) {
          _top = 170;
        } else if (parseInt(i / 4) == 2) {
          _top = 340;
        } else if (parseInt(i / 4) == 3) {
          _top = 510;
        }
      }
      this.data.nums.push({
        num: i+1,
        left: _left,
        top: _top,
      })
      this.setData({
        nums: this.data.nums
      })
    }
  },
  move: function (e) {
    var _x=e.target.dataset.x;
    var _y = e.target.dataset.y;
    var emptypoint = this.data.emptypoint;
    var temp = this.data.emptypoint;
    if ((Math.abs(_x - emptypoint[0]) == 170 && _y == emptypoint[1]) || (Math.abs(_y - emptypoint[1]) == 170 && _x == emptypoint[0])){
      this.data.emptypoint = [this.data.nums[e.target.id].left, this.data.nums[e.target.id].top]
      this.data.nums[e.target.id].left = temp[0];
      this.data.nums[e.target.id].top = temp[1];
        this.setData({
          nums: this.data.nums
        })
      }else{
      console.log(emptypoint[0]);
      console.log(_x);
      }
  }
})
